package com.moco.launcher.activity;

import java.util.List;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

import com.moco.launcher.R;
import com.moco.launcher.alarm.Alarm;
import com.moco.launcher.alarm.AlarmListAdapter;
import com.moco.launcher.alarm.BaseActivity;
import com.moco.launcher.alarm.Database;
import com.oazon.common.Logger;

public class AlarmListActivity extends BaseActivity implements OnClickListener {
	private ImageButton mBtnCancel, mBtnAdd;
	private ListView mLstViewAlarm;
	private AlarmListAdapter mListAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alarm_activity_list);
		
		mBtnCancel = (ImageButton) findViewById(R.id.alarm_cancel);
		mBtnAdd = (ImageButton) findViewById(R.id.alarm_add);
		mLstViewAlarm = (ListView) findViewById(R.id.alarm_list);
		
		mBtnCancel.setOnClickListener(this);
		mBtnAdd.setOnClickListener(this);
		
		callMathAlarmScheduleService();
		
		mListAdapter = new AlarmListAdapter(this, AlarmListActivity.this);
		
		mLstViewAlarm.setAdapter(mListAdapter);
		mLstViewAlarm.setLongClickable(true);
		setupListViewListener();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.alarm_cancel:
			// 关闭列表
			this.finish();
			break;
		case R.id.alarm_add:
			// 添加闹铃
			Intent newAlarmIntent = new Intent(this, AlarmAddActivity.class);
			startActivity(newAlarmIntent);
			break;
		}
	}

	@Override
	protected void onPause() {
		Database.deactivate();
		
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		updateAlarmList();
	}

	public void updateAlarmList() {
		Database.init(AlarmListActivity.this);
		final List<Alarm> alarms = Database.getAll();
		mListAdapter.setAlarms(alarms);

		runOnUiThread(new Runnable() {
			public void run() {
				// reload content
				AlarmListActivity.this.mListAdapter.notifyDataSetChanged();
				
				if (alarms.size() > 0)
					findViewById(R.id.alarm_empty).setVisibility(View.INVISIBLE);
				else
					findViewById(R.id.alarm_empty).setVisibility(View.VISIBLE);
			}
		});
	}
	
	private void setupListViewListener() {
		mLstViewAlarm
		.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> adapterView,
					View view, int position, long id) {
				Logger.d(getClass().getSimpleName(), "longclick come in");
				final Alarm alarm = (Alarm) mListAdapter.getItem(position);
				
				Builder dialog = new AlertDialog.Builder(AlarmListActivity.this);
				dialog.setTitle("Delete");
				dialog.setMessage("Delete this alarm?");
				dialog.setPositiveButton("Ok",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								Database.init(AlarmListActivity.this);
								Database.deleteEntry(alarm);
								AlarmListActivity.this.callMathAlarmScheduleService();

								updateAlarmList();
							}
						});
				dialog.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								dialog.dismiss();
							}
						});

				dialog.show();
				return true;
			}
		});	
	}
}
